package com.yuan.farmerwork.ynblog.rest;

import com.github.pagehelper.PageInfo;
import com.yuan.farmerwork.ynblog.domain.pojo.SearchBlog;
import com.yuan.farmerwork.ynblog.service.BlogService;
import com.yuan.farmerwork.ynblog.utils.constants.PageConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

/**
 * 搜索页面控制器.
 *
 * @author yjs
 * @date 2020/10/14 15:23
 */
@Controller
public class SearchController {

    @Autowired
    private BlogService blogService;

    /**
     * 搜索页面.
     *
     * @param searchwords 关键字
     * @param page        页码
     * @param map         页面参数map
     * @return
     */
    @GetMapping("/search/{page}/{searchwords}")
    public ModelAndView classify(@PathVariable String searchwords, @PathVariable Integer page, Map map) {
        map.put(PageConstants.PAGE_PARAM_KEYWORDS, searchwords);
        searchwords = "%" + searchwords + "%";
        PageInfo<SearchBlog> pageInfo = blogService.searchBlogs(searchwords, page);
        map.put(PageConstants.PAGE_PARAM_PAGE_INFO, pageInfo);
        return new ModelAndView(PageConstants.PAGE_ITEMS_SEARCH, map);
    }

    /**
     * 搜索页面.
     *
     * @param page 页码
     * @param map  页面参数map
     * @return
     */
    @GetMapping("/search/{page}")
    public ModelAndView searchnull(Map map, @PathVariable Integer page) {
        map.put(PageConstants.PAGE_PARAM_PAGE_INFO, new PageInfo());
        map.put(PageConstants.PAGE_PARAM_KEYWORDS, "");
        return new ModelAndView(PageConstants.PAGE_ITEMS_SEARCH, map);
    }

    /**
     * 搜索页面.
     *
     * @param map 页面参数map
     * @return
     */
    @GetMapping("/search/")
    public ModelAndView searchnull2(Map map) {
        map.put(PageConstants.PAGE_PARAM_PAGE_INFO, new PageInfo());
        map.put(PageConstants.PAGE_PARAM_KEYWORDS, "");
        return new ModelAndView(PageConstants.PAGE_ITEMS_SEARCH, map);
    }
}
